package com.example.customize.service.simple;

import com.example.customize.dao.UserDao;
import com.example.customize.entity.User;
import com.example.customize.entity.UserQuery;
import com.example.customize.entity.UserVo;
import com.example.customize.entity.base.dto.Page;
import com.example.customize.service.base.CrudServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * user Service
 * @author robot
 * @version 2020-05-27
 */
@Service
public class UserService extends CrudServiceImpl<User,UserVo,UserQuery> {
	@Autowired
	private UserDao userDao;
	
	public Page<UserVo> query4page(int pageNum, int pageSize){
		UserQuery userQuery = new UserQuery();
		userQuery.buildPageSql(pageNum, pageSize);
		List<UserVo> list = this.findList(userQuery);
		long totalCount = this.findCount(userQuery);
		Page<UserVo> page = new Page<UserVo>(pageNum, pageSize, totalCount, list);
		return page;
	}
	
	
	public Page<UserVo> query4page(UserQuery userQuery){
		if(userQuery == null){
			userQuery = new UserQuery();
		}
		if(StringUtils.isEmpty(userQuery.getSortSql())){
			userQuery.buildSortSql("order by a.create_date desc");
		}else{
			userQuery.buildSortSql(userQuery.getSortSql());
		}
		userQuery.buildPageSql();
		List<UserVo> list = this.findList(userQuery);
		long totalCount = this.findCount(userQuery);
		Page<UserVo> page = new Page<UserVo>(userQuery.getPageNum(), userQuery.getPageSize(), totalCount, list);
		return page;
	}
	
	public Page<UserVo> query4pagehelper(int pageNum, int pageSize){
		UserQuery userQuery = new UserQuery();
		PageHelper.startPage(pageNum, pageSize);
        List<UserVo> list = this.findList(userQuery);
        Page<UserVo> page = new Page<UserVo>(list);
        return page;
	}
}